/*
 * @Description:
 * @Author: your name
 * @version:
 * @Date: 2024-07-26 15:15:37
 * @LastEditors: your name
 * @LastEditTime: 2024-07-26 15:15:40
 */
import api from './request'
import { lineColors } from '@/store/staticData'
import { CoordTransform } from '@/cesiumTools/mapPlugin.js'

export const getLine = async (params) => {
  try {
    const { data, code } = await api.get(`/getLine`, { params })
    if (code === 200) {
      const result = data.map((item, index) => {
        const { xs, ys, stationsList } = item
        const colorIndex = index % lineColors.length
        const color = lineColors[colorIndex]
        item.color = color
        item.checked = true
        // 整理坐标
        const xpos = xs.split(',').map((item) => parseFloat(item))
        const ypos = ys.split(',').map((item) => parseFloat(item))
        let positions = []
        if (xpos.length === ypos.length) {
          xpos.forEach((item, index) => {
            const [lng, lat] = CoordTransform.GCJ02ToWGS84(item, ypos[index])

            positions.push({
              lng,
              lat,
            })
          })
        }
        item.paths = positions

        if (stationsList.length) {
          item.stationsList = stationsList.map((station) => {
            const { xy_coords, ...rest } = station
            const [lng, lat] = xy_coords.split(';').map((item) => Number(item))
            const [lngWgs84, latWgs84] = CoordTransform.GCJ02ToWGS84(lng, lat)
            return { position: { lng: lngWgs84, lat: latWgs84 }, ...rest }
          })
        }

        return item
      })
      return result
    } else {
      return []
    }
  } catch (error) {
    console.error(error)
  }
}
export const getLinePlan = (params) => api.get(`/getLinePlan`, { params })
export const getStationInfo = (params) => api.get(`/getStationInfo`, { params })

// https://restapi.amap.com/v3/weather/weatherInfo?parameters
// https://restapi.amap.com/v3/weather/weatherInfo?key=ad19256b1091ff1e3e92cd6d55240907&city=420100
export const getWeather = async () => {
  const params = {
    // 老师提供: 934bdde4c3d4738b0abd16bdffd0b8f7
    key: 'ad19256b1091ff1e3e92cd6d55240907',
    city: '420100',
  }

  // 打破之前的误区, get也可以携带参数, 携带的参数会作为查询参数(?key=ad19256b1091ff1e3e92cd6d55240907&city=420100)添加到url后面
  const data = await api.get(
    'https://restapi.amap.com/v3/weather/weatherInfo',
    { params }
  )
  // console.log(data);
  return data
}
